<template>
  <div class="container">
    <div class="left">
      <div class="left-top">
        <div class="logo"></div>
        <span class="logo-content">时光校园</span>
      </div>
      <div class="left-bottom">
        <div class="bottom__content">
          <div class="first-line">“每一件失物，都是未完待续的故事，</div>
          <div class="second-line">让丢失成为重逢的开始。”</div>
        </div>
        <div class="other-msg">
          <div class="item">
            <div class="iconfont icon-right-1"></div>
            <span>极速匹配</span>
          </div>
          <div class="item">
            <div class="iconfont icon-right-1"></div>
            <span>精准寻物</span>
          </div>
          <div class="item">
            <div class="iconfont icon-right-1"></div>
            <span>零基础操作</span>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="form">
        <div style="padding: 30px; background-color: white; border-radius: 5px">
          <div
            style="
              text-align: center;
              font-size: 22px;
              margin-bottom: 20px;
              color: #333;
            "
          >
            登录你的时光帐户
          </div>
          <el-form :model="form" :rules="rules" ref="formRef">
            <el-form-item prop="username">
              <el-input
                prefix-icon="el-icon-user"
                placeholder="请输入账号"
                v-model="form.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                prefix-icon="el-icon-lock"
                placeholder="请输入密码"
                show-password
                v-model="form.password"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item>
              <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
                <el-option label="管理员" value="ADMIN"></el-option>
                <el-option label="用户" value="USER"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button
                style="
                  width: 100%;
                  background-color: #2a82e4;
                  border-color: #2a82e4;
                  color: white;
                "
                @click="login"
                >登 录</el-button
              >
            </el-form-item>
            <div style="display: flex; align-items: center">
              <div style="flex: 1"></div>
              <div style="flex: 1; text-align: right; display: inline-flex">
                还没有账号？请
                <router-link style="margin-left: 4px" to="/register"
                  >注册</router-link
                >
              </div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { validator: this.vaildateUsername, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: this.validatePasswordFormat, trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    validatePasswordFormat(rule, value, callback) {
      if (value === '123456') {
        callback()
      }
      if (/^\w{6,10}$/.test(value)) {
        callback();
      } else {
        callback(new Error("密码长度为6-10个字符"));
      }
    },
    vaildateUsername(rule, value, callback) {
      if (value === 'admin') {
        callback()
      }
      if (/^\w{6,12}$/.test(value)) {
        callback();
      } else {
        callback(new Error("用户名长度为6-12个字符"));
      }
    },
    login() {
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
          // 验证通过

          if (
            this.form.username === "admin" &&
            this.form.password === "123456"
          ) {
            this.form.role = "ADMIN";
          } else {
            this.form.role = "USER";
          }

          this.$request.post("/login", this.form).then((res) => {
            if (res.code === "200") {
              localStorage.setItem("xm-user", JSON.stringify(res.data)); // 存储用户数据
              if (res.data.role === "ADMIN") {
                this.$router.push("/");
              } else {
                this.$router.push("/front/home");
              }
              this.$message.success("登录成功");
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/background.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;

  .left {
    flex: 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;

    .left-top {
      display: inline-flex;
      width: 100%;
      padding: 50px;
      align-items: center;

      .logo {
        background: url("@/assets/imgs/logo.png") center -11px/150% no-repeat;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        overflow: hidden;
      }

      .logo-content {
        font-size: 22px;
        color: #fff;
        font-family: "Kingsoft_Cloud_Font";
        margin-left: 12px;
      }
    }

    .left-bottom {
      margin-top: 10%;

      .bottom__content {
        color: #000;
        font-size: 24px;
        font-weight: 700;
        font-family: "Kingsoft_Cloud_Font";

        .second-line {
          margin-left: 12%;
        }
      }

      .other-msg {
        margin-top: 30px;
        margin-left: 30px;
        display: flex;
        gap: 8px;

        .item {
          display: inline-flex;
          align-items: center;
          gap: 3px;
        }

        .iconfont {
          color: #fff;
        }
      }
    }
  }

  .right {
    flex: 2;

    .form {
      margin-left: 80px;
      width: 340px;
      padding: 10px;
      background-color: white;
      border-radius: 5px;
    }
  }
}

a {
  color: #2a60c9;
}
</style>
